package com.sgb.kjwl.view.ui.activity.packers;

import android.content.Context;
import android.content.Intent;
import android.graphics.Typeface;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.core.content.ContextCompat;
import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.bumptech.glide.Glide;
import com.google.android.material.tabs.TabLayout;
import com.sgb.alimaplibrary.util.ToastUtil;
import com.sgb.kjwl.AppActivity;
import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.AtMapFindPackersBinding;
import com.sgb.kjwl.di.packers.DaggerPackersComponent;
import com.sgb.kjwl.di.packers.PackersModule;
import com.sgb.kjwl.model.entity.FindPackersEntity;
import com.swgk.core.util.PickSelectEntity;
import com.sgb.kjwl.model.entity.packers.PickResultEntity;
import com.sgb.kjwl.view.ui.activity.manage.ShopInfoForCompanyTradesActivity;
import com.sgb.kjwl.view.ui.activity.self.packers.PersonShopActivity;
import com.sgb.kjwl.view.ui.adapter.packers.FindPackersAdapter;
import com.sgb.kjwl.view.widget.MapFindPackersFilterPopWindow;
import com.sgb.kjwl.view.widget.PickPopWindow;
import com.sgb.kjwl.viewmodel.packers.MapFindPackersModel;
import com.swgk.core.base.di.AppComponent;
import com.swgk.core.recyclerview.adapter.AdapterOnItemClick;
import com.swgk.core.util.MToast;
import com.swgk.core.xrecyclerview.XRecyclerView;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

import javax.inject.Inject;

/**
 * @author liwei
 * @date 2020/11/10
 * description:找工队
 */
@Deprecated
public class MapFindPackersActivity extends AppActivity {

    private AtMapFindPackersBinding binding;

    @Inject
    public MapFindPackersModel viewModel;

    private FindPackersAdapter findPackersAdapter;

    private MapFindPackersFilterPopWindow popWindow;
    private PickPopWindow pickMoneyPopWindow;
    private PickPopWindow pickBusinessPopWindow;
    private PickPopWindow pickChildPopWindow;

    private PickSelectEntity all;

    private final List<PickSelectEntity> proData = new ArrayList<>();
    private final List<PickSelectEntity> majorData = new ArrayList<>();
    private final List<PickSelectEntity> businessData = new ArrayList<>();
    private final List<PickSelectEntity> businessRightData = new ArrayList<>();
    private final List<PickSelectEntity> childRightData = new ArrayList<>();
    private final List<PickSelectEntity> childData = new ArrayList<>();

    /**
     * 工程分类
     */
    private String firstCategoryNo = "";
    /**
     * 工程专项
     */
    private PickResultEntity pickResultMajor;
    /**
     * 专业分类
     */
    private PickResultEntity pickResultBusiness;
    /**
     * 分项工程
     */
    private PickResultEntity pickResultChild;

    private Drawable imgUp;
    private Drawable imgDown;
    private Drawable imgSelectDown;

    private String name;
    private String cityNo;

    public static void start(Context context, String name, String cityNo) {
        Intent intent = new Intent();
        intent.putExtra("name", name);
        intent.putExtra("cityNo", cityNo);
        intent.setClass(context, MapFindPackersActivity.class);
        context.startActivity(intent);
    }

    @Override
    protected boolean statusBarDark() {
        return false;
    }

    @Override
    protected void setupActivityComponent(AppComponent appComponent) {
        DaggerPackersComponent.builder().appComponent(appComponent).packersModule(new PackersModule(this)).build().inject(this);
    }

    private void initFilterData(boolean isLoadMore) {
        viewModel.getNewList("", firstCategoryNo, pickResultMajor.getNo(), pickResultBusiness.getNo(), pickResultChild.getNo(), "", "", "", cityNo, isLoadMore);
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (name != null && !"".equals(name)) {
            mActionBar.setTitle(name);
        }
        mActionBar.setMenuIcon(R.drawable.shaixuan);
        mActionBar.setMenuClick(v -> {
            popWindow.setMajorData(majorData, pickResultMajor);
            popWindow.setBusinessData(businessData, pickResultBusiness, false);
            popWindow.setChildData(childData, pickResultChild, false);

            popWindow.showPopwindow(binding.actionBar);
        });
    }

    @Override
    protected void initView() {
        binding = DataBindingUtil.setContentView(this, R.layout.at_map_find_packers);
        binding.setViewModel(viewModel);
        binding.tvMajor.setOnClickListener(this);
        binding.tvBusiness.setOnClickListener(this);
        binding.tvChild.setOnClickListener(this);

        if (findPackersAdapter == null) {
            findPackersAdapter = new FindPackersAdapter(getActivity(), null, Glide.with(this));
        }
        findPackersAdapter.setOnItemClick(new AdapterOnItemClick<FindPackersEntity>() {
            @Override
            public void onItemClick(FindPackersEntity findPackersEntity, int position) {
                if (findPackersEntity.getIsCompany() == 0) {
                    PersonShopActivity.start(getActivity(),findPackersEntity.getNo(),findPackersEntity.getUserNo(), findPackersEntity.getCreateTime(),"");
                } else {
                    if (TextUtils.isEmpty(findPackersEntity.getCompanyId())) {
                        ToastUtil.show(getActivity(), "企业店铺id不能为空");
                        return;
                    }
                    ShopInfoForCompanyTradesActivity.startAct(getActivity(), findPackersEntity.getCompanyId(), "", 4, 0);
                }
            }
        });
        all = new PickSelectEntity();
        all.setNo("");
        all.setProjectscale(getString(R.string.app_all));
        DividerItemDecoration divider = new DividerItemDecoration(this, DividerItemDecoration.VERTICAL);
        divider.setDrawable(Objects.requireNonNull(ContextCompat.getDrawable(this, R.drawable.shape_list_divide)));
        binding.recyclerView.addItemDecoration(divider);
        LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
        binding.recyclerView.setLayoutManager(layoutManager);
        binding.recyclerView.setAdapter(findPackersAdapter);
        binding.recyclerView.setLoadingMoreEnabled(true);
        binding.recyclerView.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                initFilterData(false);
            }

            @Override
            public void onLoadMore() {
                initFilterData(true);

            }
        });


        imgUp = ContextCompat.getDrawable(this,R.mipmap.ic_up_arrow);
        if (imgUp != null) {
            imgUp.setBounds(0, 0, imgUp.getMinimumWidth(), imgUp.getMinimumHeight());
        }
        imgSelectDown = ContextCompat.getDrawable(this,R.mipmap.ic_down_arrow);
        if (imgSelectDown != null) {
            imgSelectDown.setBounds(0, 0, imgSelectDown.getMinimumWidth(), imgSelectDown.getMinimumHeight());
        }
        imgDown = ContextCompat.getDrawable(this,R.mipmap.ic_down_arrow_n);
        if (imgDown != null) {
            imgDown.setBounds(0, 0, imgDown.getMinimumWidth(), imgDown.getMinimumHeight());
        }

        pickResultMajor = new PickResultEntity();
        pickResultBusiness = new PickResultEntity();
        pickResultChild = new PickResultEntity();
    }

    @Override
    protected void init() {
        name = getIntent().getStringExtra("name");
        cityNo = getIntent().getStringExtra("cityNo");

        popWindow = new MapFindPackersFilterPopWindow(MapFindPackersActivity.this);
        popWindow.setBgView(binding.grayLayout);
        popWindow.setFilterResultClick((pickResultM, pickResultB, pickResultC) -> {
            if (!TextUtils.equals(pickResultMajor.getNo(), pickResultM.getNo())) {
                businessData.clear();
                businessData.addAll(businessRightData);
                childData.clear();
                childData.addAll(childRightData);
            } else if (!TextUtils.equals(pickResultBusiness.getNo(), pickResultB.getNo())) {
                childData.clear();
                childData.addAll(childRightData);
            }
            pickResultMajor.replace(pickResultM);
            pickResultBusiness.replace(pickResultB);
            pickResultChild.replace(pickResultC);
            if (!"".equals(pickResultM.getName())) {
                if (getString(R.string.app_all).equals(pickResultM.getName())) {
                    binding.tvMajor.setText("工程专项");
                    binding.tvMajor.setTextColor(ContextCompat.getColor(this,R.color.color_666666));
                    binding.tvMajor.setCompoundDrawables(null, null, imgDown, null);
                    binding.tvBusiness.setText("专业分类");
                    binding.tvBusiness.setTextColor(ContextCompat.getColor(this,R.color.color_666666));
                    binding.tvBusiness.setCompoundDrawables(null, null, imgDown, null);
                    binding.tvChild.setText("分项工程");
                    binding.tvChild.setTextColor(ContextCompat.getColor(this,R.color.color_666666));
                    binding.tvChild.setCompoundDrawables(null, null, imgDown, null);
                } else {
                    binding.tvMajor.setText(pickResultM.getName());
                    binding.tvMajor.setTextColor(ContextCompat.getColor(this,R.color.color_main));
                    binding.tvMajor.setCompoundDrawables(null, null, imgSelectDown, null);
                }
            }
            if (!"".equals(pickResultB.getName())) {
                if (getString(R.string.app_all).equals(pickResultB.getName())) {
                    binding.tvBusiness.setText("专业分类");
                    binding.tvBusiness.setTextColor(ContextCompat.getColor(this,R.color.color_666666));
                    binding.tvBusiness.setCompoundDrawables(null, null, imgDown, null);
                    binding.tvChild.setText("分项工程");
                    binding.tvChild.setTextColor(ContextCompat.getColor(this,R.color.color_666666));
                    binding.tvChild.setCompoundDrawables(null, null, imgDown, null);
                } else {
                    binding.tvBusiness.setText(pickResultB.getName());
                    binding.tvBusiness.setTextColor(ContextCompat.getColor(this,R.color.color_main));
                    binding.tvBusiness.setCompoundDrawables(null, null, imgSelectDown, null);
                }
            }
            if (!"".equals(pickResultC.getName())) {
                if (getString(R.string.app_all).equals(pickResultC.getName())) {
                    binding.tvChild.setText("分项工程");
                    binding.tvChild.setTextColor(ContextCompat.getColor(this,R.color.color_666666));
                    binding.tvChild.setCompoundDrawables(null, null, imgDown, null);
                } else {
                    binding.tvChild.setText(pickResultC.getName());
                    binding.tvChild.setTextColor(ContextCompat.getColor(this,R.color.color_main));
                    binding.tvChild.setCompoundDrawables(null, null, imgSelectDown, null);
                }
            }
            initFilterData(false);
        });

        // 工程专项
        pickMoneyPopWindow = new PickPopWindow(MapFindPackersActivity.this);
        pickMoneyPopWindow.setBgView(binding.grayLayout);
        pickMoneyPopWindow.setOnResultClick((key1, key2, key3) -> {
            pickResultMajor.setNo((String) key1);
            pickResultMajor.setName((String) key2);
            pickResultMajor.setPosition((int) key3);
            if (TextUtils.isEmpty((String) key1) || TextUtils.equals(getString(R.string.app_all), (String) key2)) {
                binding.tvMajor.setText("工程专项");
            } else {
                binding.tvMajor.setText((String) key2);
            }
            pickResultBusiness.clear();
            binding.tvBusiness.setText("专业分类");
            pickBusinessPopWindow.setDefData(pickResultBusiness.getPosition());
            binding.tvBusiness.setTextColor(ContextCompat.getColor(this,R.color.color_666666));
            binding.tvBusiness.setCompoundDrawables(null, null, imgDown, null);
            pickResultChild.clear();
            binding.tvChild.setText("分项工程");
            pickChildPopWindow.setDefData(pickResultChild.getPosition());
            binding.tvChild.setTextColor(ContextCompat.getColor(this,R.color.color_666666));
            binding.tvChild.setCompoundDrawables(null, null, imgDown, null);
            initFilterData(false);
            viewModel.getThrCategory((String) key1, false);
        });
        pickMoneyPopWindow.setOnDismissListener(() -> {
            if (TextUtils.isEmpty(pickResultMajor.getNo())) {
                binding.tvMajor.setTextColor(ContextCompat.getColor(this,R.color.color_666666));
                binding.tvMajor.setCompoundDrawables(null, null, imgDown, null);
            } else {
                binding.tvMajor.setTextColor(ContextCompat.getColor(this,R.color.color_main));
                binding.tvMajor.setCompoundDrawables(null, null, imgSelectDown, null);
            }
        });

        // 专业分类
        pickBusinessPopWindow = new PickPopWindow(MapFindPackersActivity.this);
        pickBusinessPopWindow.setBgView(binding.grayLayout);
        pickBusinessPopWindow.setOnResultClick((key1, key2, key3) -> {
            pickResultBusiness.setNo((String) key1);
            pickResultBusiness.setName((String) key2);
            pickResultBusiness.setPosition((int) key3);
            if (TextUtils.isEmpty((String) key1) || TextUtils.equals(getString(R.string.app_all), (String) key2)) {
                binding.tvBusiness.setText("专业分类");
            } else {
                binding.tvBusiness.setText((String) key2);
            }
            pickResultChild.clear();
            binding.tvChild.setText("分项工程");
            pickChildPopWindow.setDefData(pickResultChild.getPosition());
            binding.tvChild.setTextColor(ContextCompat.getColor(this,R.color.color_666666));
            binding.tvChild.setCompoundDrawables(null, null, imgDown, null);
            initFilterData(false);
            viewModel.getFourCategory((String) key1, false);
        });
        pickBusinessPopWindow.setOnDismissListener(() -> {
            if (TextUtils.isEmpty(pickResultBusiness.getNo())) {
                binding.tvBusiness.setTextColor(ContextCompat.getColor(this,R.color.color_666666));
                binding.tvBusiness.setCompoundDrawables(null, null, imgDown, null);
            } else {
                binding.tvBusiness.setTextColor(ContextCompat.getColor(this,R.color.color_main));
                binding.tvBusiness.setCompoundDrawables(null, null, imgSelectDown, null);
            }
        });

        //子项工程
        pickChildPopWindow = new PickPopWindow(this);
        pickChildPopWindow.setOnResultClick((key1, key2, key3) -> {
            pickResultChild.setNo((String) key1);
            pickResultChild.setName((String) key2);
            pickResultChild.setPosition((int) key3);
            if (TextUtils.isEmpty((String) key1) || TextUtils.equals(getString(R.string.app_all), (String) key2)) {
                binding.tvChild.setText("分项工程");
            } else {
                binding.tvChild.setText((String) key2);
            }
            initFilterData(false);
        });
        pickChildPopWindow.setOnDismissListener(() -> {
            if (TextUtils.isEmpty(pickResultChild.getNo())) {
                binding.tvChild.setTextColor(ContextCompat.getColor(this,R.color.color_666666));
                binding.tvChild.setCompoundDrawables(null, null, imgDown, null);
            } else {
                binding.tvChild.setTextColor(ContextCompat.getColor(this,R.color.color_main));
                binding.tvChild.setCompoundDrawables(null, null, imgSelectDown, null);
            }
        });

        //列表
        initFilterData(false);
        //工程分类 一级分类
        viewModel.getProTypeList();
        //工程分类 二级分类
        viewModel.getSecondCategory("");
    }

    @Override
    public void onMultiClick(View v) {
        super.onMultiClick(v);
        int id = v.getId();
        if (id == R.id.tv_major) {
            binding.tvMajor.setTextColor(ContextCompat.getColor(this,R.color.color_main));
            binding.tvMajor.setCompoundDrawables(null, null, imgUp, null);
            showMajorPopWindow();
        } else if (id == R.id.tv_business) {
            if ("".equals(pickResultMajor.getNo())) {
                MToast.showToast(MapFindPackersActivity.this, "请先选择工程专项");
            } else {
                binding.tvBusiness.setTextColor(ContextCompat.getColor(this,R.color.color_main));
                binding.tvBusiness.setCompoundDrawables(null, null, imgUp, null);
                showBusinessPopWindow();
            }
        } else if (id == R.id.tv_child) {
            if ("".equals(pickResultBusiness.getNo())) {
                MToast.showToast(MapFindPackersActivity.this, "请先选择专业分类");
            } else {
                setTextViewChange(binding.tvChild);
                showChildPopWindow();
            }
        } else if (id == R.id.map_view) {
            PackersMapActivity.start(MapFindPackersActivity.this, 0);
        }
    }

    public void showList(List<FindPackersEntity> list, boolean isLoadMore, boolean isLast) {
        if (isLoadMore) {
            binding.recyclerView.loadMoreComplete();
            if (list != null && list.size() > 0) {
                findPackersAdapter.addDatas(list);
            }
            binding.recyclerView.setNoMore(isLast);
        } else {
            binding.recyclerView.refreshComplete();
            if (list != null && list.size() > 0) {
                binding.emptyView.setVisibility(View.GONE);
                findPackersAdapter.setDatas(list);
            } else {
                findPackersAdapter.clearDatas();
                binding.emptyView.setVisibility(View.VISIBLE);
            }
        }
    }


    /**
     * 工程类型
     * @param data 工程类型pop的列表数据
     */
    public void setProData(List<PickSelectEntity> data) {
        proData.clear();
        data.add(0, all);
        proData.addAll(data);
        initTab(data);
    }

    private void initTab(List<PickSelectEntity> data) {
        binding.tabLayout.setTabMode(TabLayout.MODE_SCROLLABLE);
        for (int i = 0; i < data.size(); i++) {
            binding.tabLayout.addTab(binding.tabLayout.newTab().setCustomView(R.layout.item_tablayout_view_2));
            TextView tvTab = binding.tabLayout.getTabAt(i).getCustomView().findViewById(R.id.tv_tab_item);
            tvTab.setTextColor(ContextCompat.getColor(MapFindPackersActivity.this, R.color.color_666666));
            tvTab.setTypeface(Typeface.defaultFromStyle(Typeface.NORMAL));
            tvTab.setText(data.get(i).getProjectscale());
            if (i == 0) {
                tvTab.setTextColor(ContextCompat.getColor(MapFindPackersActivity.this, R.color.color_main));
                tvTab.setTypeface(Typeface.defaultFromStyle(Typeface.BOLD));
            }
        }
        binding.tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                TextView tv = tab.getCustomView().findViewById(R.id.tv_tab_item);
                tv.setTypeface(Typeface.defaultFromStyle(Typeface.BOLD));//加粗
                tv.setTextColor(ContextCompat.getColor(MapFindPackersActivity.this, R.color.color_main));
                tv.invalidate();
                firstCategoryNo = proData.get(tab.getPosition()).getNo();
                pickResultMajor.clear();
                pickResultBusiness.clear();
                pickMoneyPopWindow.setDefData(pickResultMajor.getPosition());
                pickBusinessPopWindow.setDefData(pickResultBusiness.getPosition());
                binding.tvMajor.setText("工程专项");
                binding.tvMajor.setTextColor(ContextCompat.getColor(MapFindPackersActivity.this,R.color.color_666666));
                binding.tvMajor.setCompoundDrawables(null, null, imgDown, null);
                binding.tvBusiness.setText("专业分类");
                binding.tvBusiness.setTextColor(ContextCompat.getColor(MapFindPackersActivity.this,R.color.color_666666));
                binding.tvBusiness.setCompoundDrawables(null, null, imgDown, null);
                initFilterData(false);
                viewModel.getSecondCategory(firstCategoryNo);
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {
                TextView tv = tab.getCustomView().findViewById(R.id.tv_tab_item);
                tv.setTypeface(Typeface.defaultFromStyle(Typeface.NORMAL));
                tv.setTextColor(ContextCompat.getColor(MapFindPackersActivity.this, R.color.color_666666));
                tv.invalidate();
            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });
    }

    /**
     * 工程专项pop
     * @param data 工程专项pop的列表数据
     */
    public void setMajorData(List<PickSelectEntity> data) {
        majorData.clear();
        data.add(0, all);
        majorData.addAll(data);
    }

    /**
     * 专业类型pop
     * @param data 专业类型pop的列表数据
     * @param isRight 是否在右边的评判标准
     */
    public void setBusinessData(List<PickSelectEntity> data, boolean isRight) {
        if (isRight) {
            businessRightData.clear();
            data.add(0, all);
            businessRightData.addAll(data);
            popWindow.setBusinessData(data, pickResultBusiness, true);
        } else {
            businessData.clear();
            data.add(0, all);
            businessData.addAll(data);
        }
    }

    /**
     * 分项工程
     * @param data 分项工程pop的列表数据
     * @param isRight 是否在右边的评判标准
     */
    public void setChildData(List<PickSelectEntity> data, boolean isRight) {
        if (isRight) {
            childRightData.clear();
            data.add(0, all);
            childRightData.addAll(data);
            popWindow.setChildData(data, pickResultChild, true);
        } else {
            childData.clear();
            data.add(0, all);
            childData.addAll(data);
        }
    }

    /**
     * 工程专项pop
     */
    private void showMajorPopWindow() {
        pickMoneyPopWindow.showPopwindow(binding.tvMajor);
        pickMoneyPopWindow.setData(majorData, pickResultMajor.getPosition());
    }

    /**
     * 专业类型
     */
    private void showBusinessPopWindow() {
        pickBusinessPopWindow.showPopwindow(binding.tvBusiness);
        pickBusinessPopWindow.setData(businessData, pickResultBusiness.getPosition());
    }

    /**
     * 分项工程pop
     */
    private void showChildPopWindow() {
        pickChildPopWindow.showPopwindow(binding.tvChild);
        pickChildPopWindow.setData(childData, pickResultChild.getPosition());
    }

    private void setTextViewChange(TextView textView) {
        Drawable images = ContextCompat.getDrawable(this, R.mipmap.ic_up_arrow);
        if (images != null){
            images.setBounds(0, 0, images.getMinimumWidth(), images.getMinimumHeight());
        }
        textView.setCompoundDrawables(null, null, images, null);
        textView.setTextColor(ContextCompat.getColor(this, R.color.color_main));
    }
}
